<template>
  <div id="userLoginPage">
    <div class="login-container">
      <div class="login-header">
        <h1 class="title">木棉 AI 代码生成器</h1>
        <div class="desc">不写一行代码，生成完整应用</div>
      </div>

      <div class="login-form-wrapper">
        <h2 class="form-title">用户登录</h2>
        <a-form
          :model="formState"
          name="basic"
          autocomplete="off"
          @finish="handleSubmit"
          layout="vertical"
        >
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input
              v-model:value="formState.userAccount"
              placeholder="请输入账号"
              size="large"
              :prefix="h(UserOutlined)"
            />
          </a-form-item>

          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码不能小于 8 位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              placeholder="请输入密码"
              size="large"
              :prefix="h(LockOutlined)"
            />
          </a-form-item>

          <div class="login-options">
            <a-checkbox>记住我</a-checkbox>
            <a class="forgot-password">忘记密码？</a>
          </div>

          <a-form-item>
            <a-button type="primary" html-type="submit" size="large" class="login-button"
              >登录</a-button
            >
          </a-form-item>

          <div class="register-link">
            没有账号？<RouterLink to="/user/register" class="signup-link">立即注册</RouterLink>
          </div>

          <div class="other-login-methods">
            <div class="divider">
              <span>其他登录方式</span>
            </div>
            <div class="social-login">
              <a-button shape="circle" class="social-icon">
                <template #icon><WechatOutlined /></template>
              </a-button>
              <a-button shape="circle" class="social-icon">
                <template #icon><QqOutlined /></template>
              </a-button>
              <a-button shape="circle" class="social-icon">
                <template #icon><GithubOutlined /></template>
              </a-button>
            </div>
          </div>
        </a-form>
      </div>

      <div class="login-footer">
        <p>© 2023 木棉 AI 代码生成器 - 编程导航原创项目</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, h } from 'vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { userLogin } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import {
  UserOutlined,
  LockOutlined,
  WechatOutlined,
  QqOutlined,
  GithubOutlined,
} from '@ant-design/icons-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})
const router = useRouter()
const loginUserStore = useLoginUserStore()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const res = await userLogin(values)
  // 登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
#userLoginPage {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 75vh;
  background: linear-gradient(
    135deg,
    #4ade80 0%,
    #22c55e 25%,
    #16a34a 50%,
    #15803d 75%,
    #166534 100%
  );
  padding: 20px;
  position: relative;
}

.login-container {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px 30px;
  position: relative;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  backdrop-filter: blur(10px);
}

.login-container:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.login-header {
  text-align: center;
  margin-bottom: 36px;
}

.title {
  font-size: 28px;
  font-weight: 700;
  color: #16a34a;
  margin-bottom: 12px;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.desc {
  color: #8c8c8c;
  font-size: 16px;
  margin-top: 8px;
}

.form-title {
  font-size: 22px;
  font-weight: 500;
  margin-bottom: 28px;
  color: #262626;
  text-align: center;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
}

.forgot-password {
  color: #1890ff;
  cursor: pointer;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 6px;
}

.register-link {
  text-align: center;
  margin: 20px 0 28px;
  color: #8c8c8c;
  font-size: 14px;
}

.signup-link {
  color: #1890ff;
  font-weight: 500;
  margin-left: 4px;
}

.other-login-methods {
  margin-top: 20px;
}

.divider {
  display: flex;
  align-items: center;
  color: #bfbfbf;
  font-size: 14px;
  margin: 16px 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #f0f0f0;
}

.divider span {
  padding: 0 16px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
}

.social-icon {
  color: #8c8c8c;
  font-size: 18px;
  transition: all 0.3s;
}

.social-icon:hover {
  color: #1890ff;
  background: #f0f7ff;
}

.login-footer {
  text-align: center;
  margin-top: 40px;
  color: #bfbfbf;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 30px 20px;
    box-shadow: none;
  }

  .title {
    font-size: 24px;
  }

  .form-title {
    font-size: 20px;
    margin-bottom: 24px;
  }

  .login-button {
    height: 40px;
  }
}
</style>
